Completed
Push — master ( 76852c...676732 )
by Sander
03:09
created

angular.controller(ꞌVaultCtrlꞌ)   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 210

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 4
nop 10
dl 0
loc 210
rs 8.2857

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
	/**
27
	 * @ngdoc function
28
	 * @name passmanApp.controller:MainCtrl
29
	 * @description
30
	 * # MainCtrl
31
	 * Controller of the passmanApp
32
	 */
33
	angular.module('passmanApp')
34
		.controller('VaultCtrl', ['$scope', 'VaultService', 'SettingsService', 'CredentialService', '$location', 'ShareService', 'EncryptService', '$translate', '$rootScope', '$interval',
35
			function ($scope, VaultService, SettingsService, CredentialService, $location, ShareService, EncryptService, $translate, $rootScope, $interval) {
36
			VaultService.getVaults().then(function (vaults) {
37
				$scope.vaults = vaults;
38
				if (SettingsService.getSetting('defaultVault') != null) {
0 ignored issues
show
Coding Style introduced by
It is recommended to use !== to compare with null.

Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.

Read more about comparison operations.

Loading history...
39
					var default_vault = SettingsService.getSetting('defaultVault');
40
41
					/**
42
					 * Using a native for loop for preformance reasons.
43
					 * More info see http://stackoverflow.com/questions/13843972/angular-js-break-foreach
44
					 */
45
					for (var i = 0; i < vaults.length; i++) {
46
						var vault = vaults[i];
47
						if (vault.guid === default_vault.guid) {
48
							$scope.default_vault = true;
49
							$scope.list_selected_vault = vault;
50
							SettingsService.setSetting('defaultVault', vault);
51
							if (SettingsService.getSetting('defaultVaultPass')) {
52
								$location.path('/vault/' + vault.guid);
53
							}
54
							$scope.vault_tries[vault.guid] = {
55
								tries: 0,
56
								timeout: 0
57
							};
58
							break;
59
						}
60
					}
61
				}
62
			});
63
64
65
			var key_strengths = [
66
				'password.poor',
67
				'password.poor',
68
				'password.weak',
69
				'password.good',
70
				'password.strong'
71
			];
72
73
			$scope.default_vault = false;
74
			$scope.remember_vault_password = false;
75
			$scope.auto_logout_timer = false;
76
			$scope.logout_timer = '0';
77
			$scope.list_selected_vault = false;
78
			$scope.minimal_value_key_strength = 3;
79
80
			var settingsLoaded = function () {
81
				$scope.minimal_value_key_strength = SettingsService.getSetting('vault_key_strength');
82
				$translate(key_strengths[SettingsService.getSetting('vault_key_strength')]).then(function (translation) {
83
					$scope.required_score = {'strength': translation};
84
				});
85
			};
86
87
			if (!SettingsService.getSetting('settings_loaded')) {
88
				$rootScope.$on('settings_loaded', function () {
89
					settingsLoaded();
90
				});
91
			} else {
92
				settingsLoaded();
93
			}
94
95
			$scope.toggleDefaultVault = function () {
96
				$scope.default_vault = !$scope.default_vault;
97
				if ($scope.default_vault === true) {
98
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
99
				} else {
100
					SettingsService.setSetting('defaultVault', null);
101
				}
102
			};
103
104
			$scope.toggleRememberPassword = function () {
105
				$scope.remember_vault_password = !$scope.remember_vault_password;
106
				if ($scope.remember_vault_password) {
107
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
108
					$scope.default_vault = true;
109
				}
110
				if ($scope.remember_vault_password !== true) {
111
					SettingsService.setSetting('defaultVault', null);
112
				}
113
			};
114
115
			$scope.toggleAutoLogout = function () {
116
				$scope.auto_logout_timer = !$scope.auto_logout_timer;
117
			};
118
119
			$scope.clearState = function () {
120
				$scope.list_selected_vault = false;
121
				$scope.creating_vault = false;
122
				$scope.error = false;
123
			};
124
125
			$scope.selectVault = function (vault) {
126
				$scope.list_selected_vault = vault;
127
				if(!$scope.vault_tries[vault.guid]) {
128
					$scope.vault_tries[vault.guid] = {
129
						tries: 0,
130
						timeout: 0
131
					};
132
				}
133
			};
134
			$scope.sharing_keys = {};
135
			$scope.newVault = function () {
136
				$scope.creating_vault = true;
137
				var key_size = 1024;
138
				ShareService.generateRSAKeys(key_size).progress(function (progress) {
139
					var p = progress > 0 ? 2 : 1;
140
					var msg = $translate.instant('generating.sharing.keys');
141
					msg = msg.replace('%step', p);
142
					$scope.creating_keys = msg;
143
					$scope.$digest();
144
				}).then(function (kp) {
145
					var pem = ShareService.rsaKeyPairToPEM(kp);
146
					$scope.creating_keys = false;
147
					$scope.sharing_keys.private_sharing_key = pem.privateKey;
148
					$scope.sharing_keys.public_sharing_key = pem.publicKey;
149
					$scope.$digest();
150
				});
151
152
			};
153
154
			var _loginToVault = function (vault, vault_key) {
155
				var _vault = angular.copy(vault);
156
				_vault.vaultKey = angular.copy(vault_key);
157
				delete _vault.credentials;
158
				var timer = parseInt($scope.logout_timer);
159
				if ($scope.auto_logout_timer && timer > 0) {
160
					$rootScope.$broadcast('logout_timer_set', timer * 60);
161
				}
162
163
				VaultService.setActiveVault(_vault);
164
				$location.path('/vault/' + vault.guid);
165
			};
166
167
			$scope.selectLogoutTimer = function (time) {
168
				$scope.auto_logout_timer = true;
169
				$scope.logout_timer = time;
170
			};
171
172
			var tickLockTimer = function (guid) {
173
				$scope.vault_tries[guid].timeout = $scope.vault_tries[guid].timeout - 1;
174
				if($scope.vault_tries[guid].timeout <= 0){
175
					$interval.cancel($scope.vault_tries[guid].timer);
176
					$scope.vault_tries[guid].timeout = 0;
177
				}
178
			};
179
180
			$scope.vault_tries = {};
181
182
			$scope.vaultDecryptionKey = '';
183
			$scope.loginToVault = function (vault, vault_key) {
184
				$scope.error = false;
185
				var _vault = angular.copy(vault);
186
				_vault.vaultKey = angular.copy(vault_key);
187
188
				VaultService.setActiveVault(_vault);
189
				try {
190
					EncryptService.decryptString(vault.challenge_password);
191
					if ($scope.remember_vault_password) {
192
						SettingsService.setSetting('defaultVaultPass', vault_key);
193
					}
194
					_loginToVault(vault, vault_key);
195
196
				} catch (e) {
197
					$scope.error = $translate.instant('invalid.vault.key');
198
199
					$scope.vault_tries[vault.guid].tries = $scope.vault_tries[vault.guid].tries + 1;
200
201
					if($scope.vault_tries[vault.guid].tries >= 3){
202
						var duration = (Math.pow(2, 1 / 7) * Math.pow(15, 4 / 7)) * Math.pow((Math.pow(2, 2 / 7) * Math.pow(15, 1 / 7)), $scope.vault_tries[vault.guid].tries);
203
						$scope.vault_tries[vault.guid].timeout = duration;
204
205
						if($scope.vault_tries[vault.guid].hasOwnProperty('timer')){
206
							$interval.cancel($scope.vault_tries[vault.guid].timer);
207
						}
208
209
						$scope.vault_tries[vault.guid].timer = $interval(function () {
210
							tickLockTimer(vault.guid);
211
						} ,1000);
212
					}
213
214
				}
215
			};
216
217
218
			$scope.createVault = function (vault_name, vault_key, vault_key2) {
219
				if (vault_key !== vault_key2) {
220
					$scope.error = $translate.instant('password.do.not.match');
221
					return;
222
				}
223
				VaultService.createVault(vault_name).then(function (vault) {
224
					$scope.vaults.push(vault);
225
					var _vault = angular.copy(vault);
226
					_vault.vaultKey = angular.copy(vault_key);
227
					VaultService.setActiveVault(_vault);
228
					SettingsService.setSetting('defaultVaultPass', null);
229
					SettingsService.setSetting('defaultVault', null);
230
					var test_credential = CredentialService.newCredential();
231
					test_credential.label = 'Test key for vault ' + vault_name;
232
					test_credential.hidden = true;
233
					test_credential.vault_id = vault.vault_id;
234
					test_credential.password = 'lorum ipsum';
235
					CredentialService.createCredential(test_credential).then(function () {
236
						_vault.public_sharing_key = angular.copy($scope.sharing_keys.public_sharing_key);
237
						_vault.private_sharing_key = EncryptService.encryptString(angular.copy($scope.sharing_keys.private_sharing_key));
238
						VaultService.updateSharingKeys(_vault).then(function () {
239
							_loginToVault(vault, vault_key);
240
						});
241
					});
242
				});
243
			};
244
		}]);
245
}());